export default {
  data() {
    return {
      planMaintainDateOptions: [],
      weekOptions: [
        { value: "1", label: "星期一" },
        { value: "2", label: "星期二" },
        { value: "3", label: "星期三" },
        { value: "4", label: "星期四" },
        { value: "5", label: "星期五" },
        { value: "6", label: "星期六" },
        { value: "7", label: "星期日" },
      ],
      dateOptions: [],
      monthDateOptions: [
        { value: 1, label: "1月", children: [] },
        { value: 2, label: "2月", children: [] },
        { value: 3, label: "3月", children: [] },
        { value: 4, label: "4月", children: [] },
        { value: 5, label: "5月", children: [] },
        { value: 6, label: "6月", children: [] },
        { value: 7, label: "7月", children: [] },
        { value: 8, label: "8月", children: [] },
        { value: 9, label: "9月", children: [] },
        { value: 10, label: "10月", children: [] },
        { value: 11, label: "11月", children: [] },
        { value: 12, label: "12月", children: [] },
      ],
    };
  },
  methods: {
    // 日期初始化选项
    setDateOptions(dateOptions) {
      for (let i = 1; i <= 31; i++) {
        dateOptions.push({ label: i + "日", value: i + "" });
      }
    },
    // 月日初始化选项
    setMonthDateOptions(monthDateOptions) {
      monthDateOptions.forEach((d) => {
        switch (d.value) {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
            for (let i = 1; i <= 31; i++) {
              d.children.push({ label: i + "日", value: i });
            }
            break;
          case 2:
            for (let i = 1; i <= 29; i++) {
              d.children.push({ label: i + "日", value: i });
            }
            break;
          case 4:
          case 6:
          case 9:
          case 11:
            for (let i = 1; i <= 30; i++) {
              d.children.push({ label: i + "日", value: i });
            }
            break;
        }
      });
    },
    getPlanMaintainDateValue(maintainCycle, planMaintainDate) {
      let value = "";
      let planMaintainDateOptions = [];
      let index = -1;
      let subIndex = -1;
      switch (maintainCycle) {
        case "month":
          planMaintainDateOptions = this.dateOptions;
          index = planMaintainDateOptions.findIndex(
            (d) => d.value === planMaintainDate[0]
          );
          if (index !== -1) {
            value = planMaintainDateOptions[index].label;
          }
          break;
        case "year":
          planMaintainDateOptions = this.monthDateOptions;
          index = planMaintainDateOptions.findIndex(
            (d) => d.value === planMaintainDate[0]
          );
          if (index !== -1) {
            subIndex = planMaintainDateOptions[index].children.findIndex(
              (d) => d.value === planMaintainDate[1]
            );
            value =
              planMaintainDateOptions[index].label +
              "/" +
              planMaintainDateOptions[index].children[subIndex].label;
          }
          break;
        case "week":
          planMaintainDateOptions = this.weekOptions;
          index = planMaintainDateOptions.findIndex(
            (d) => d.value === planMaintainDate[0]
          );
          if (index !== -1) {
            value = planMaintainDateOptions[index].label;
          }
          break;
        default:
          planMaintainDateOptions = [];
      }
      return value;
    },
    changeMaintainCycle(type) {
      let column = null;
      let data = null;
      data = this.tools.lodash.cloneDeep(this.dataSet.leftFormData);
      data.values.sf_planMaintainDate = "";
      if (type == "year") {
        data.values.sf_planMaintainDate = [];
      }
      this.$set(this.dataSet, "leftFormData", data);
      this.loadLeftForm(type);
    },
    loadLeftForm(type) {
      let column = null;

      switch (type) {
        case "month":
          this.planMaintainDateOptions = this.dateOptions;

          column = this.attrs.columns.find(
            (item) => item.key == "sf_planMaintainDate"
          );
          column.config.type = "select";
          column.config.readonly = false;
          break;
        case "year":
          this.planMaintainDateOptions = this.monthDateOptions;

          column = this.attrs.columns.find(
            (item) => item.key == "sf_planMaintainDate"
          );
          column.config.type = "cascader";
          column.config.readonly = false;
          break;
        case "week":
          this.planMaintainDateOptions = this.weekOptions;
          column = this.attrs.columns.find(
            (item) => item.key == "sf_planMaintainDate"
          );
          column.config.type = "select";
          column.config.readonly = false;
          break;
        default:
          // 清空计划维护时间选择的数据
          column = this.attrs.columns.find(
            (item) => item.key == "sf_planMaintainDate"
          );
          //   column.show = false;
          column.config.type = "text";
          column.config.readonly = true;
      }
    },
  },
};
